home *** CD-ROM | disk | FTP | other *** search
/ Freelog 9 / Freelog009.iso / BAS / Internet / Rtf2Html / Source C / RTFPARSE / MPARSE.C < prev    next >
C/C++ Source or Header  |  1999-06-27  |  8KB  |  319 lines

  1.  
  2. /*
  3.  * %%File: mparse.c
  4.  *    
  5.  * Copyright (c) 1995-1999 Bertrand LE QUELLEC
  6.  *
  7.  * http://perso.wanadoo.fr/blq
  8.  * blq@wanadoo.fr
  9.  */
  10.  
  11.  
  12. #define STRICT
  13. #include <windows.h>
  14. #include <string.h>
  15. #include <stdio.h>
  16.  
  17. #include "rtftype.h"
  18. #include "rtfdecl.h"
  19. #include "rtfreadr.h"
  20. #include "rtfparse.h"
  21. #include "html.h"                   
  22.                   
  23. #define SOURCE_MAIN 1
  24. #include "mparse.h"
  25.  
  26.     
  27.     
  28.  
  29. static void InitVariable (void)
  30. {                              
  31.     nbFigure = 0;
  32.     prefigure[0] = '\0';
  33.  
  34.     flagTableau = carcell = nbcell = oldnbcell = 0;
  35.     cellule[0] = '\0';
  36.  
  37.     plusNiveau = fontSize = 0;
  38.     oldProp = tagCenter = tagUL = tagTR = tagLI = tagDD = flagDD = 0;
  39.  
  40.     flagHeader = flagGO = flagTOC = flagREF = 0;
  41.  
  42.     crochet = valCrochetF = valCrochetH = 0;
  43.  
  44.     flagFootnote = nbFootnote = 0;
  45.  
  46.     nbcar = 0;
  47.     lesCar[0] = '\0';
  48.  
  49.     nbBk = flagBKMK = 0;
  50.     lesBkmk[0] = '\0';
  51.  
  52.     styleBold = styleItalic = valCrochetB = valCrochetI = 0;
  53.     
  54.     niveau = oldNiveau = carniv = 0;
  55.     leNiveau[0] = '\0';
  56.  
  57.     carnot = 0;
  58.     Notes[0] = lesNotes[0][0] = '\0';
  59.     nbnote = flagNote = flagPnText = nbResult = flagFldrslt = 0;
  60.     Fldrslt[0] = '\0';
  61.     
  62.     cGroup = flagStop = nbUrl = 0;
  63.     
  64.     Url[0] = '\0';
  65.     flagBR_P = flagFRAME = 0;
  66.  
  67.     cbBin = lParam = 0L;
  68.     fSkipDestIfUnk = 0;
  69. }
  70.     
  71.  
  72. /* ------------------- VISUAL C++ ------------------------------------------ */
  73. int _export _pascal
  74.     RtfToHTML(char * fichier_rtf, char * fichier_html,
  75.                 int EnteteFichier, int FinFichier, char * prefixe,
  76.                 char * titreFichier, char * couleur, char * image, int br_p, int frame)
  77. {                     
  78.     FILE * fpIndex = (FILE *)0;
  79.     int retour = ecOK, ct = 0, ct1 = 0, flag = 0;
  80.              
  81.       
  82.     InitVariable ();
  83.       
  84.     /* --- */
  85.     if(!strlen(fichier_rtf) || !fichier_rtf)
  86.         return errfRTF;  
  87.     
  88.     if(!(fpIn = (FILE *)fopen(fichier_rtf, "r")))
  89.         return erroRTF;  
  90.          
  91.          
  92.     /* --- */  
  93.     if(!strlen(fichier_html) || !fichier_html)
  94.         return errfHTML;
  95.           
  96.     if(!(fpIndex = (FILE *)fopen(fichier_html, "w")))
  97.     {
  98.         fclose(fpIn);
  99.         
  100.         return erroHTML;
  101.     }
  102.       
  103.     /* FRAME */
  104.     flagFRAME = frame;
  105.     
  106.     if (flagFRAME)
  107.     {     
  108.         if ((int)strlen(fichier_html) < MLEN*2)
  109.         {
  110.             for (ct = 0; ct < (int)strlen(fichier_html); ct++)
  111.             {                                             
  112.                 if (fichier_html[ct] == '.') break;
  113.                 if (fichier_html[ct] == '\\')
  114.                     flag = 0;
  115.                 else
  116.                     flag++;
  117.                     
  118.                 pageFile    [ct] = fichier_html[ct];
  119.                 tableFile    [ct] = fichier_html[ct];
  120.                 noteFile    [ct] = fichier_html[ct];
  121.             }
  122.             
  123.             if (flag > 5) ct = ct - 3;
  124.         }
  125.         
  126.         pageFile    [ct] = '\0';
  127.         tableFile    [ct] = '\0';
  128.         noteFile    [ct] = '\0';
  129.         
  130.         sprintf(pageFile, "%s%s", pageFile, PAGE_FILE);
  131.         sprintf(tableFile, "%s%s", tableFile, TABLE_FILE);
  132.         sprintf(noteFile, "%s%s", noteFile, NOTE_FILE);
  133.             
  134.         /* --- */ 
  135.         if(!(fpOut = (FILE *)fopen(pageFile, "w")))
  136.         {
  137.             fclose(fpIn);
  138.             
  139.             return erroHTML;
  140.         }               
  141.         
  142.         if(!(fpTable = (FILE *)fopen(tableFile, "w")))
  143.         {
  144.             fclose (fpIn);
  145.             fclose (fpOut);
  146.             
  147.             return erroHTML;
  148.         }
  149.      
  150.         if(!(fpNote = (FILE *)fopen(noteFile, "w")))
  151.         {
  152.             fclose (fpIn);
  153.             fclose (fpOut);
  154.             fclose (fpTable);
  155.             
  156.             return erroHTML;
  157.         }
  158.         
  159.         if ((int)strlen(fichier_html) < MLEN*2)
  160.         {         
  161.             pageFile    [0] = '\0';
  162.             tableFile    [0] = '\0';
  163.             noteFile    [0] = '\0';
  164.             
  165.             for (ct = (int)strlen(fichier_html); ct >= 0; ct--)
  166.             {
  167.                 if (fichier_html[ct] == '\\')
  168.                 {
  169.                     ct++;
  170.                     break;
  171.                 }
  172.             }
  173.             
  174.             for (ct1 = 0; ct < (int)strlen(fichier_html); ct++, ct1++)
  175.             {                                             
  176.                 if (fichier_html[ct] == '.') break;
  177.                 
  178.                 pageFile    [ct1] = fichier_html[ct];
  179.                 tableFile    [ct1] = fichier_html[ct];
  180.                 noteFile    [ct1] = fichier_html[ct];
  181.             }
  182.             
  183.             if (flag > 5) ct1 = ct1 - 3;
  184.             
  185.             pageFile    [ct1] = '\0';
  186.             tableFile    [ct1] = '\0';
  187.             noteFile    [ct1] = '\0';
  188.             
  189.             sprintf(pageFile, "%s%s", pageFile, PAGE_FILE);
  190.             sprintf(tableFile, "%s%s", tableFile, TABLE_FILE);
  191.             sprintf(noteFile, "%s%s", noteFile, NOTE_FILE);
  192.         }
  193.     }
  194.     else
  195.         fpOut = fpIndex;
  196.     
  197.     /* PREFIXE FIGURE */
  198.     if(prefixe && strlen(prefixe))
  199.         sprintf(prefigure, "%s", prefixe);
  200.     else
  201.         sprintf(prefigure, "figure");
  202.  
  203.     /* ENTETE */
  204.     if(EnteteFichier)
  205.     {
  206.         EnteteHTML(TAG_RTFTOHTML, fpOut);
  207.         
  208.         if (flagFRAME)
  209.         {
  210.             EnteteHTML(TAG_RTFTOHTML, fpTable);
  211.             EnteteHTML(TAG_RTFTOHTML, fpNote);
  212.         }
  213.     }
  214.     
  215.     /* TITRE */
  216.     if(strlen(titreFichier) && titreFichier)
  217.     {
  218.         TitreHTML(titreFichier, fpOut);
  219.         
  220.         if (flagFRAME)
  221.         {
  222.             TitreHTML("Table des matières", fpTable);
  223.             TitreHTML("Notes de bas de page", fpNote);
  224.         }
  225.     }
  226.  
  227.     /* BGCOLOR */
  228.     if (strlen(couleur) && couleur)
  229.     {
  230.         BgColorHTML(couleur, fpOut);
  231.         
  232.         if (flagFRAME)
  233.         {
  234.             BgColorHTML(couleur, fpTable);
  235.             BgColorHTML(couleur, fpNote);
  236.         }
  237.     }
  238.  
  239.     /* BACKGROUND */
  240.     if(strlen(image) && image)
  241.     {
  242.         BackgroundHTML(image, fpOut);
  243.         
  244.         if (flagFRAME)
  245.         {
  246.             BackgroundHTML(image, fpTable);
  247.             BackgroundHTML(image, fpNote);
  248.         }
  249.     }
  250.  
  251.     /* BR ou P */
  252.     flagBR_P = br_p;
  253.     
  254.     if (flagFRAME)
  255.     {
  256.         fprintf(fpTable, "<H3>Table des matières</H3>");
  257.         fprintf(fpNote, "<H3>Notes de bas de page</H3>");
  258.     }
  259.     
  260.     /* ------------------------ 
  261.      *  Lancement du parseur : */
  262.     retour = ecRtfParse(fpIn);
  263.  
  264.     if(FinFichier)
  265.     {
  266.         fprintf(fpOut, "\n%s%s", TAGf_BODY, TAGf_HTML);
  267.         
  268.         if (flagFRAME)
  269.         {
  270.             fprintf(fpTable, "\n%s%s", TAGf_BODY, TAGf_HTML);
  271.             fprintf(fpNote, "\n%s%s", TAGf_BODY, TAGf_HTML);
  272.         }
  273.     }
  274.  
  275.     fflush(fpOut);
  276.     
  277.     if (flagFRAME)
  278.     {
  279.         fflush(fpTable);
  280.         fflush(fpNote);
  281.         
  282.         fclose (fpTable);
  283.         fclose (fpNote);
  284.     }
  285.     
  286.     fclose (fpIn);
  287.     fclose (fpOut);
  288.     
  289.     if (!flagFRAME) return retour;
  290.     
  291.     /* --- */
  292.     
  293.     fprintf(fpIndex, "%s\n%s\n%s\n%s\n", TAG_HTML, TAG_HEAD, TAG_RTFTOHTML, TAGf_HEAD);
  294.     
  295.     if (flagNote > 0)
  296.     {      
  297.         fprintf(fpIndex, "<FRAMESET ROWS=\"85%%,15%%\" NAME=\"principal\"\nFRAMEBORDER=\"NO\" FRAMESPACING=0 BORDER=1 SCROLLING=\"auto\">\n");
  298.           
  299.         fprintf(fpIndex, "<FRAMESET COLS=\"30%%,70%%\" NAME=\"document\"\nFRAMEBORDER=\"NO\" FRAMESPACING=0 BORDER=0 SCROLLING=\"auto\">\n");
  300.         fprintf(fpIndex, "\t<FRAME SRC=\"%s\" NAME=\"%s\">\n", tableFile, TABLE_FRAME);
  301.         fprintf(fpIndex, "\t<FRAME SRC=\"%s\" NAME=\"%s\">\n</FRAMESET>\n", pageFile, PAGE_FRAME);
  302.             
  303.         fprintf(fpIndex, "\t<FRAME SRC=\"%s\" NAME=\"%s\">\n</FRAMESET>\n", noteFile, NOTE_FRAME);
  304.     }
  305.     else
  306.     {
  307.         fprintf(fpIndex, "<FRAMESET COLS=\"30%%,70%%\" NAME=\"principal\"\nFRAMEBORDER=\"NO\" FRAMESPACING=0 BORDER=0 SCROLLING=\"auto\">\n");
  308.         fprintf(fpIndex, "\t<FRAME SRC=\"%s\" NAME=\"%s\">\n", tableFile, TABLE_FRAME);
  309.         fprintf(fpIndex, "\t<FRAME SRC=\"%s\" NAME=\"%s\">\n</FRAMESET>\n", pageFile, PAGE_FRAME);
  310.     }                                                          
  311.     
  312.     fprintf(fpIndex, "\n%s\n", TAGf_HTML);
  313.     
  314.     fflush (fpIndex);
  315.     fclose (fpIndex);
  316.     
  317.     return retour;
  318. }
  319.